Re: [SQL] Re: [HACKERS] Counting bool flags in a complex query - Mailing list pgsql-sql

From Herouth Maoz
Subject Re: [SQL] Re: [HACKERS] Counting bool flags in a complex query
Date
Msg-id l03130303b3b8c4d43b74@[147.233.159.109]
Whole thread Raw
In response to Re: [HACKERS] Counting bool flags in a complex query  (Michael Richards <miker@scifair.acadiau.ca>)
Responses Re: [SQL] Re: [HACKERS] Counting bool flags in a complex query  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
At 11:37 +0300 on 16/07/1999, Michael Richards wrote:


> My folder numbers are: negative numbers are system folders such as New
> mail, trash, drafts and sentmail. I wanted to order the tuples so that the
> folderids were sorted from -1 to -4, then 1 to x. This way the system
> folders would always appear first in the list.
>
> This may not be valid SQL, as none of my books mention it. Is it possible
> to order by an expression?
>
> Here are some examples which some some odd behaviour. My suspected bug
> findings are at the end:

I think the problem results from using non-standard constructs such as
order by expression, and indeed ordering by columns that don't appear in
the select list.

If you want to do the best by yourself, put the expression by which you
order in the select list. A simple example would be:

Instead of: SELECT f1, min( f2 ), max ( f3 ) GROUP BY f1 ORDER BY expr( f1 );

Use:
 SELECT expr( f1 ) AS ordcol, f1, min( f2 ), max( f3 ) GROUP BY ordcol, f1 ORDER BY ordcol;

What is the difference? The difference is that now GROUP BY (which also
does internal sorting) knows about that expression and considers it. Since
ordcol is the same for each value of f1, this should not change the groups.
This simply makes sure all parts of the query are aware of what is being
done around them. This is also the standard, as far as I recall.

What's the problem? You have a column in the output that you didn't really
want. But hey, why should that bother you? If you're reading it through
some frontend, simply have it ignore the first column that returns.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma




pgsql-sql by date:

Previous
From: Herouth Maoz
Date:
Subject: Re: [SQL] SELECT using arrays
Next
From: secret
Date:
Subject: Re: [SQL] Good Optimization